package com.snailct.bpp.db;


import org.springframework.util.StringUtils;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huangxa on 2016/7/20.
 */
public class MyBatisPojo implements Serializable {
    @Id
    private Integer id;
    public String id() {
        for(Field field : this.getClass().getDeclaredFields()) {
            if(field.isAnnotationPresent(Id.class))
                return field.getName();
        }

        throw new RuntimeException("undefine POJO @Id");
    }
    // 以@Column标识的字段,表示这个字段是一般列
    public String tablename() {
        Table table = (Table)this.getClass().getAnnotation(Table.class);
        if(table != null)
            return table.name();
        else{
            throw new RuntimeException();
        }
    }
    private transient static Map<Class<? extends MyBatisPojo>,List<String>> columnMap = new HashMap<Class<? extends MyBatisPojo>, List<String>>();
    public void caculationColumnList() {
        if(columnMap.containsKey(this.getClass()))
            return;

        Field[] fields = this.getClass().getDeclaredFields();
        List<String> columnList = new ArrayList<String>(fields.length);

        for(Field field : fields) {
            if(field.isAnnotationPresent(Column.class))
                columnList.add(field.getName());
        }

        columnMap.put(this.getClass(), columnList);
    }
    /**
     * 用于获取Insert的字段累加
     * @return
     */
    public String returnInsertColumnsName() {
        StringBuilder sb = new StringBuilder();

        List<String> list = columnMap.get(this.getClass());
        int i = 0;
        for(String column : list) {
            if(column==null)
                continue;

            if(i++ != 0)
                sb.append(',');
            sb.append(column);
        }
        return sb.toString();
    }
    /**
     * 用于获取Insert的字段映射累加
     * @return
     */
    public String returnInsertColumnsDefine() {
        StringBuilder sb = new StringBuilder();

        List<String> list = columnMap.get(this.getClass());
        int i = 0;
        for(String column : list) {
            if(column==null)
                continue;

            if(i++ != 0)
                sb.append(',');
            sb.append("#{").append(column).append('}');
        }
        return sb.toString();
    }

    public String returnUpdateSet() {
        StringBuilder sb = new StringBuilder();
        List<String> list = columnMap.get(this.getClass());
        int i = 0;
                for(String column : list) {
                        if(column==null||"".equals(column))
                                continue;

                        if(i++ != 0)
                                sb.append(',');
                        sb.append(column).append("=#{").append(column).append('}');
                    }
                return sb.toString();

    }

        public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}
